package ma.view.gui.parameterPanels.mwgde;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import ma.controller.panels.auxillary.CtrInformationPanel;
import ma.model.MWGDEInterface;
import ma.view.gui.parameterPanels.ProcessParameterPanel;

public abstract class ProcessPanelMWGDE
    extends ProcessParameterPanel implements ActionListener {
    
    private static final long serialVersionUID = 1L;
    private List<String> materials;
    private JComboBox<String> materialsList;
    private MWGDEInterface mwgdeManuMethod;
    
    public ProcessPanelMWGDE(
            String logoPath,
            String manuMethodName,
            MWGDEInterface manuMethod,
            CtrInformationPanel infoPanelCtr) {
        
        super(logoPath, manuMethodName);
        mwgdeManuMethod = manuMethod;
        materials = mwgdeManuMethod.getMaterials();
        addMaterialPanel();
        update();
    }
    
    private void addMaterialPanel() {
        parametersPanel.add(new JLabel("Werkstofftyp: "));
        String[] materialsArray =
                materials.toArray(new String[materials.size()]);
        materialsList = new JComboBox<String>(materialsArray);
        materialsList.addActionListener(this);        
        materialsList.setSelectedIndex(getActiveMaterialIndex());
        String material = (String) materialsList.getSelectedItem();
        mwgdeManuMethod.setMaterial(
                mwgdeManuMethod.stringToMaterial(material));        
        parametersPanel.add(materialsList, "wrap");
    }
    
    private int getActiveMaterialIndex() {
        String materialSet = mwgdeManuMethod
                .materialToString(mwgdeManuMethod.getMaterial());
        for(String mat : materials) {
            if(mat.equals(materialSet)) {
                return materials.indexOf(mat);
            }
        }
        return 0;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {        
        if(e.getSource() == materialsList) {
            String material = (String) materialsList.getSelectedItem();
            mwgdeManuMethod
                .setMaterial(mwgdeManuMethod.stringToMaterial(material));
        }
    }
    
    @Override
    /* (non-Javadoc)
     * @see ma.view.gui.ObserverInterface#update(ma.view.gui.ObserverInterface)
     */    
    public void update() {
        
        /*
         * Feed f and feed rate v_f should not both be set by the user.
         * Both parameters depend on each other. So, the user gets the chance
         * to set only one parameter. The text field of the other gets disabled
         * then.
         */
        JTextField field_f = textFields.get("f");
        JTextField field_vf = textFields.get("v_f");
        
        if(field_f == null || field_vf == null) {
        	return;
        }
        
        if(mwgdeManuMethod.getParameter("f").getDependence()) {
        	field_f.setEnabled(false);
        } else if(mwgdeManuMethod.getParameter("v_f").getDependence()) {
        	field_vf.setEnabled(false);
        } else {
        	field_f.setEnabled(true);        
        	field_vf.setEnabled(true);
        }
        /*
        if (field_f != null && field_vf != null) {
            if(field_vf.getText().equals("") &&
                    !field_f.getText().equals("")) {
                field_vf.setEnabled(false);
                field_f.setEnabled(true);
            } else if(field_f.getText().equals("") &&
                    !field_vf.getText().equals("")) {
                field_f.setEnabled(false);
                field_vf.setEnabled(true);
            } else if(field_f.getText().equals("") &&
                    field_vf.getText().equals("")) {
                field_f.setEnabled(true);
                field_vf.setEnabled(true);
            }
        } */       
    }   
}
